import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import Index from '../views/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component:Index
  },
   {
    path: '/gongjian',
    name: 'GongJian',
    component: () => import('../views/GongJian.vue')
  },
    {
    path: '/test',
    name: 'Index',
    component:()=>import('../views/test.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue') ,meta: {
      hideHeader: true,
    },
  },
    {
    path: '/lianxi',
    name: 'LianXi',
    component: () => import('../views/lxwm.vue')
  },
    {
    path: '/news',
    name: 'News',
    component: () => import('../views/News.vue')
  },
  {
    path: '/wenzhang',
    name: 'wenzahng',
    component: () => import('../views/WenZhang.vue')
  },
  
    {
    path: '/dianmian',
      name: 'DianMian',
     meta: {
        requireAuth:true
      },
    component: () => import('../views/dianmian.vue')
  },
    {
    path: '/xiangqing',
   name: 'Index',
    component: () => import('../views/Sun.vue')
  },
  {
    path: '/jiament',
    name: 'Jiament',
    redirect: '/jiament/youshi',
      meta: {
        requireAuth:true
      },
    component: () => import('../views/JiaMeng.vue'),
    children: [{
      path: 'youshi',
      name: 'Jiament',
     
      component:()=>import('../views/YouShi.vue')
    },
      {
        path: 'liucheng',
        name: 'Jiament',
      component:()=>import('../views/jialiu.vue')
    },
      {
        path: 'feiyong',
        name: 'Jiament',
      component:()=>import('../views/FeiYong.vue')
      },
       {
         path: 'zhichi',
         name: 'Jiament',
      component:()=>import('../views/ZhiChi.vue')
      },
         {
           path: 'yiwu',
           name: 'Jiament',
      component:()=>import('../views/YiWu.vue')
    },
    ] 

  },
   {
    path: '/login',
    name: 'Login',
     component: () => import('../views/Login.vue'),
     meta: {
      hideHeader: true,
    },
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 验证to的路由是否需要验证
  // console.log(to.matched)
  if (to.matched.some(record=>record.meta.requireAuth)) {
    // 判断store中用户信息是否存储
    if (!store.state.uname) {
      return next({name:'Login'})
    }
    next()
  } else {
    next()
  }
})

export default router
